﻿Ext.define("PSI.Truck.MainForm", {
    setCloseFlag: function (closeFlag) {
        this.closeFlag = closeFlag;
    },

    createToolButtons: function () {
        var me = this;
        var p = me.permission;
        var result = [];
        result.push("-");
        result.push({ text: "新增拖车公司", iconCls: "psi-button-add", handler: me.onAddTruckCompany, scope: me });
        result.push("-");
        result.push({ text: "编辑拖车公司", iconCls: "psi-button-edit", handler: me.onEditTruckCompany, scope: me });
        result.push("-");
        result.push({ text: "新增拖车", iconCls: "psi-button-add", handler: me.onAddTruck, scope: me });
        result.push("-");
        result.push({ text: "编辑拖车", iconCls: "psi-button-edit", handler: me.onEditTruck, scope: me });
        result.push("-")
        result.push({ text: "微信禁用", iconCls: "psi-button-edit", handler:function(){me.wxJY(0)} , scope: me });
        result.push("-")
        result.push({ text: "解除禁用", iconCls: "psi-button-edit", handler: function () { me.wxJY(1) }, scope: me });
        result.push("-")
        result.push({ text: "重置密码", iconCls: "psi-button-edit", handler: me.czMM, scope: me });
        result.push("-");
        result.push({ text: "刷新", iconCls: "psi-button-refresh", handler: me.onRefreshPlanGird, scope: me });
        result.push("-");
        result.push({
            text: "关闭", iconCls: "psi-button-exit", handler: function () {
                if (me.closeFlag === "1") {
                    if (Ext.isChrome) {
                        window.open("", "_self", "");
                        window.close();
                    } else if (Ext.isIE) {
                        window.close();
                    } else {
                        location.replace("/");
                    }
                } else {
                    location.replace("/");
                }
            }
        });
        return result;
    },

    wxJY: function (stuta) {
        var me = this;
        var items = me.getTruckCompanyGrid().getSelectionModel().getSelection();
        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择要操作的拖车公司");
            return;
        }
        PSI.MsgBox.confirm("是否执行此操作", function () {
            var entity = items[0].getData().id;
            Ext.Ajax.request({
                url: "/Truck/DisablePWD",
                params: {
                    truckid: entity,
                    flag: stuta
                },
                method: "POST",
                success: function (response) {
                    var data = Ext.JSON.decode(response.responseText);
                    if (data == "") {
                        var mess = stuta == 1 ? "密码已解除禁用" : "密码已禁用";
                        PSI.MsgBox.showInfo(mess, function () {
                            me.refreshTruckCompanyGrid();
                        });
                    }
                    else
                    {
                        PSI.MsgBox.showInfo(data);
                    }
                }

            });
        })
         
    },

    czMM:function(){
        var me = this;
        var items = me.getTruckCompanyGrid().getSelectionModel().getSelection();
        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择要操作的拖车公司");
            return;
        }
        PSI.MsgBox.confirm("是否执行此操作", function () {
            var entity = items[0].getData().id;
            Ext.Ajax.request({
                url: "/Truck/ResetPWD",
                params: {
                    truckid: entity
                },
                method: "POST",
                success: function (response) {
                    var data = Ext.JSON.decode(response.responseText);
                    if (data == "") {
                        PSI.MsgBox.showInfo("密码已重置！", function () {
                            me.refreshTruckCompanyGrid();
                        });
                    }
                    else {
                        PSI.MsgBox.showInfo(data);
                    }
                }

            });
        })

    },

    createUI: function () {
        var me = this;

        var panel = Ext.create("Ext.panel.Panel", {
            border: 0,
            layout: "border",
            title: "拖车",
            tbar: me.createToolButtons(),
            items: [
                {
                    region: "west", xtype: "container", layout: "fit", border: false,
                    width: 500, split: true,
                    items: [me.getTruckCompanyGrid()]
                }, {
                    region: "center", xtype: "container", layout: "fit", border: false,
                    items: [me.getTruckGrid()]
                }
            ]
        });

        me.panel = panel;

        me.getTruckCompanyGrid().on("selectionchange", me.onRefreshTruckGrid, me);

        me.refreshTruckCompanyGrid();
    },

    getMainPanel: function () {
        return this.panel;
    },

    getTruckCompanyGrid: function () {
        var me = this;

        if (!me.__TruckCompanyGrid) {
            me.__TruckCompanyGrid = me.createTruckCompanyGrid();
        }

        return me.__TruckCompanyGrid;
    },

    createTruckCompanyGrid: function () {
        var modelName = "PSITruckCompanyModel";
        Ext.define(modelName, {
            extend: "Ext.data.Model",
            fields: ["id", "name", "v_owner", "n_tel","i_weixinStatus"]
        });

        return Ext.create("Ext.grid.Panel", {
            viewConfig: { enableTextSelection: true },
            columnLines: true,
            columns: [
                     Ext.create("Ext.grid.RowNumberer", { text: "", width: 30 }),
                    { header: "拖车公司", dataIndex: "name", menuDisabled: true, flex: 1 },
                    { header: "车主", dataIndex: "v_owner" },
                    { header: "车主手机", dataIndex: "n_tel" },
                    {
                        header: "是否禁用", dataIndex: "i_weixinStatus",
                        renderer: function (value) {
                            if(value==1) 
                                return "未禁用"
                            else
                                return "已禁用"
                        }
                    }
            ],
            store: Ext.create("Ext.data.Store", {
                autoLoad: false,
                model: modelName,
                data: []
            })
        });
    },

    getTruckGrid: function () {
        var me = this;

        if (!me.__TruckGrid) {
            me.__TruckGrid = me.createTruckGrid();
        }

        return me.__TruckGrid;
    },

    createTruckGrid: function () {
        var modelName = "PSITruckModel";
        Ext.define(modelName, {
            extend: "Ext.data.Model",
            fields: ["id", "truckNumber", "driver", "driverTel"]
        });

        return Ext.create("Ext.grid.Panel", {
            viewConfig: { enableTextSelection: true },
            columnLines: true,
            columns: [
                     Ext.create("Ext.grid.RowNumberer", { text: "", width: 30 }),
                    { header: "车号", dataIndex: "truckNumber", menuDisabled: true, width: 150 },
                    { header: "司机", dataIndex: "driver", menuDisabled: true, width: 150 },
                    { header: "司机电话", dataIndex: "driverTel", menuDisabled: true, width: 150 }
            ],
            store: Ext.create("Ext.data.Store", {
                autoLoad: false,
                model: modelName,
                data: []
            })
        });
    },

    refreshTruckCompanyGrid: function (selectedId) {
        var me = this;
        var grid = me.getTruckCompanyGrid();
        var store = grid.getStore();
        var items = grid.getSelectionModel().getSelection();
        if (selectedId == null && items && items.length > 0) {
            selectedId = items[0].getData().id;
        }
        var el = grid.getEl() || Ext.getBody();
        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/Truck/GetTruckCompanyList",
            method: "POST",
            callback: function (options, success, response) {
                store.removeAll();

                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    store.add(data);

                    if (selectedId) {
                        for (var i = 0; i < store.getCount() ; i++) {
                            var m = store.getAt(i);
                            if (m.get("id") == selectedId) {
                                grid.getSelectionModel().select(m);
                                break;
                            }
                        }
                    } else {
                        if (data.length > 0) {
                            grid.getSelectionModel().select(0);
                        }
                    }
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }

                el.unmask();
            }
        });
    },

    onRefreshTruckGrid: function () {
        this.refreshTruckGrid();
    },

    refreshTruckGrid: function (selectedId) {
        var me = this;
        me.getTruckGrid().getStore().removeAll();

        var items = me.getTruckCompanyGrid().getSelectionModel().getSelection();
        if (items == null || items.length == 0) {
            return;
        }

        var parentId = items[0].getData().id;

        var grid = me.getTruckGrid();
        var store = grid.getStore();
        var items = grid.getSelectionModel().getSelection();
        if (selectedId == null && items && items.length > 0) {
            selectedId = items[0].getData().id;
        }
        var el = grid.getEl() || Ext.getBody();
        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/Truck/GetTruckList",
            params: { id: parentId },
            method: "POST",
            callback: function (options, success, response) {
                store.removeAll();

                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    store.add(data);

                    if (selectedId) {
                        for (var i = 0; i < store.getCount() ; i++) {
                            var m = store.getAt(i);
                            if (m.get("id") == selectedId) {
                                grid.getSelectionModel().select(m);
                                break;
                            }
                        }
                    } else {
                        if (data.length > 0) {
                            grid.getSelectionModel().select(0);
                        }
                    }
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }

                el.unmask();
            }
        });
    },

    onAddTruckCompany: function () {
        var form = Ext.create("PSI.Truck.TruckCompanyEditForm");
        form.show(this,null,"self");
    },

    onEditTruckCompany: function () {
        var me = this;
        var items = me.getTruckCompanyGrid().getSelectionModel().getSelection();
        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择要编辑的拖车公司");
            return;
        }

        var entity = items[0].getData();

        var form = Ext.create("PSI.Truck.TruckCompanyEditForm");
        form.show(me, entity,null);
    },

    onAddTruck: function () {
        var me = this;
        var items = me.getTruckCompanyGrid().getSelectionModel().getSelection();
        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择一个拖车公司");
            return;
        }
        var truckComapny = items[0].getData();
        var form = Ext.create("PSI.Truck.TruckEditForm");
        form.show(me, truckComapny);
    },

    onEditTruck: function () {
        var me = this;
        var items = me.getTruckCompanyGrid().getSelectionModel().getSelection();
        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择一个拖车公司");
            return;
        }
        var truckComapny = items[0].getData();

        var items = me.getTruckGrid().getSelectionModel().getSelection();
        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择一个要编辑的拖车");
            return;
        }
        var truck = items[0].getData();

        var form = Ext.create("PSI.Truck.TruckEditForm");
        form.show(me, truckComapny, truck);
    }
});